import { ListStore } from '@/store';
import { INITIAL_FORM_QUERY_VALUE, LIST_NAME, LOAD_AUDIT_PRICE_LIST_REQUEST_TASK_NAME, INITIAL_FORM_VALUE } from '@/views/script-settings/constants';
import { loadListStore, enable, disable } from './api';
import { message } from '@/utils/util';
import { getChinaRegionUrlApi } from '@/views/cellular-area-config/api';
import { REGION_CITY_STORAGE_KEY } from '@/constants';
import { errorHandler, setLocalStorageByExpire, getLocalStorageByExpire } from '@/utils';

export default class Store extends ListStore {
  formQueryValue = { ...INITIAL_FORM_QUERY_VALUE };

  querying = false;

  visible = false;

  taskId = '';

  // 当前编辑商品参数 - 用于每次打开Modal时作为其表单初始值传入
  formValue = { ...INITIAL_FORM_VALUE };

  importVisible = false;

  cellularList = [];

  cellularMap = new Map();

  cityList = []; // 省市区列表

  // 加载省市列表
  loadRegionCitys = async () => {
    const storageData = getLocalStorageByExpire(REGION_CITY_STORAGE_KEY);
    if (storageData && Object.prototype.toString.call(storageData) === '[object Array]' && storageData.length > 0) {
      this.cityList = storageData;
      return;
    }

    try {
      const result = (await getChinaRegionUrlApi()) || [];
      const citys = result.map(item => {
        return {
          label: item.region_name,
          value: item.region_name,
          children:
            item.children?.map(childItem => {
              return {
                label: childItem.region_name,
                value: childItem.region_name,
                children:
                  childItem.children?.map(childChildItem => {
                    return {
                      label: childChildItem.region_name,
                      value: childChildItem.region_name,
                      children: undefined,
                    };
                  }) || undefined,
              };
            }) || undefined,
        };
      });

      if (citys.length > 0) {
        // 设置7天有效期
        setLocalStorageByExpire(REGION_CITY_STORAGE_KEY, citys);
        this.cityList = citys;
      } else {
        this.cityList = [];
      }
    } catch (error) {
      errorHandler({ error, showErrorNotification: false });
      this.cityList = [];
    }
  };

  // 加载列表
  loadDataList = async ({ loadAction = 'reset', pageSize: inputPageSize, pageNum: inputPageNum } = {}) => {
    await this.loadList({
      requestTaskName: LOAD_AUDIT_PRICE_LIST_REQUEST_TASK_NAME,
      listName: LIST_NAME,
      loadAction,
      pageSize: inputPageSize,
      pageNum: inputPageNum,
      loadDataFunc: async ({ pageSize, pageNum }) => {
        const result = await loadListStore(this.formQueryValue, { pageSize, pageNum });
        let { rows: data = [], total = 0 } = result || {};
        return { data, total };
      },
    });
  };
  // 查询
  queryList = value => {
    this.formQueryValue = value;
    this.loadDataList({ loadAction: 'pageNumChange', pageNum: 1 });
  };

  // 当前页变更
  pageNumChange = pageNum => {
    this.loadDataList({ loadAction: 'pageNumChange', pageNum });
  };

  // 每页条数变更
  pageSizeChange = pageSize => {
    this.loadDataList({ loadAction: 'pageSizeChange', pageSize });
  };

  resetTable = () => {
    this.loadDataList({ loadAction: 'reset' });
  };
  //关闭新增编辑对话框
  onClose = () => {
    this.visible = false;
    this.formValue = { ...INITIAL_FORM_VALUE };
    if (this.formQueryValue.topChannelCode) {
      this.resetTable();
    }
  };
  //打开新增编辑对话框
  onOpenEditModal = value => {
    this.visible = true;
    if (value) {
      this.formValue = value;
    }
  };
  // 启用
  onEnable = async id => {
    try {
      await enable(id);
      message('提示', '操作成功', 'success');
      this.resetTable();
    } catch (e) {
      message('提示', '操作失败', 'error');
    }
  };
  // 禁用
  onDisable = async id => {
    try {
      await disable(id);
      message('提示', '操作成功', 'success');
      this.resetTable();
    } catch (e) {
      message('提示', '操作失败', 'error');
    }
  };

  // 初始加载数据
  init = async () => {
    await this.loadRegionCitys();
    await this.loadDataList({ loadAction: 'reset' });
    // await this.listCellular();
  };
}
